package com.neu.controller;

import com.neu.pojo.Commodity;
import com.neu.service.CommodityService;
import com.neu.service.CommodityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @RequestMapping("delCommodity")
    @ResponseBody
    public HashMap delCommodity(String commodity) {
        int res = commodityService.delCommodity(commodity);
        HashMap<String, Object> map = new HashMap<>();
        if (res > 0) {
            map.put("code", 0);
            map.put("msg", "删除成功");
        } else {
            map.put("code", -1);
            map.put("msg", "删除失败，请稍后重试.");
        }
        return map;
    }

    @RequestMapping("addCommodity")
    @ResponseBody
    public HashMap addCommodity(Commodity commodity){
        int res=commodityService.addCommodity(commodity);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "新建成功");
        }else{
            map.put("code", -1);
            map.put("msg", "新建失败，请稍后重试.");
        }
        return map;
    }

    @RequestMapping("getCommodityByPage")
    @ResponseBody
    public HashMap getCommodityByPage(int page,int limit){
        List<Commodity> list=commodityService.getCommodityByPage(page, limit);
        int totalCount=commodityService.getCommodityCount();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",totalCount);
        map.put("data",list);
        return map;
    }


    @RequestMapping("updateCommodity")
    @ResponseBody
    public HashMap updateCommodity(Commodity commodity){
        int res=commodityService.updateCommodity(commodity);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "更新成功");
        }else{
            map.put("code", -1);
            map.put("msg", "更新失败，请稍后重试.");
        }
        return map;
    }
}
